package com.zhhe.linearList;

/*
 *Author:ZouHeng
 *Des:双向链表
 *Date:2020-12-09  15:51
 */
public class DoubleLinkedList<T> implements IMyList<T> {
    private DoubleNode<T> head;
    private int size;

    public DoubleLinkedList() {
        this.size = 0;
        this.head = null;
    }

    @Override
    public T get(int index) {
        if (index < 0 || index > size) {
            try {
                throw new Exception("索引越界，无法获取元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        DoubleNode<T> node = head;
        for (int i = 0; i < index; i++) {
            node = node.getNext();
        }
        return node.getValue();
    }

    @Override
    public void add(T t) {
        if (size == 0) {
            head.setValue(t);
            head.setNext(null);
            head.setPrev(null);
        } else {
            DoubleNode<T> tailNode = new DoubleNode<>(t);
            DoubleNode<T> node = head;
            while (node.getNext() != null) {
                node = node.getNext();
            }
            node.setNext(tailNode);
            tailNode.setNext(null);
            tailNode.setPrev(node);
        }
        size++;
    }

    @Override
    public void add(int index, T t) {
        if (index < 0 || index > size) {
            try {
                throw new Exception("索引越界，无法插入元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        DoubleNode<T> newNode = new DoubleNode<>(t);
        if (index == 0) {
            DoubleNode<T> node = head;
            head = newNode;
            newNode.setNext(node);
        } else {
            DoubleNode<T> prevNode = head;
            for (int i = 0; i < index; i++) {
                if (i > 0) {
                    prevNode = prevNode.getNext();
                }
            }
            DoubleNode<T> node = prevNode.getNext();
            prevNode.setNext(newNode);
            newNode.setNext(node);
        }
        size++;
    }

    @Override
    public void remove(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception("索引越界，无法删除元素，链表的长度为" + size + "，删除的索引为" + index);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (index == 0) {
            if (size == 1) {
                head.setValue(null);
                head.setNext(null);
            } else {
                DoubleNode<T> node = head.getNext();
                head.setValue(node.getValue());
                head.setNext(node.getNext());
            }
        } else {
            DoubleNode<T> prevNode = head;    //删除节点的前一个节点
            DoubleNode<T> nextNode = null;    //删除节点的后一个节点
            for (int i = 0; i < index; i++) {
                if (i > 0) {
                    prevNode = prevNode.getNext();
                }
                nextNode = prevNode.getNext().getNext();
            }
            prevNode.setNext(nextNode);
        }
        size--;
    }

    @Override
    public void removeAll() {
        head.setNext(null);
        head.setValue(null);
        this.size = 0;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    public void print() {
        if (isEmpty()) {
            System.out.println("单链表为空");
        } else {
            System.out.println("SingleLinkedList{" + head.toString() + "}");
        }
    }

    public static void main(String[] args) {
        DoubleLinkedList<Integer> list = new DoubleLinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.print();
    }
}
